package com.example.healthmanagement.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.healthmanagement.R;
import com.example.healthmanagement.adapter.LoseWeightAdapter;
import com.example.healthmanagement.bean.LoseWeightBean;
import com.example.healthmanagement.util.OkHttpTool;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 健康资讯
 */
public class LoseWeightActivity extends AppCompatActivity {
    private Activity myActivity;//上下文
    private RecyclerView rvList;
    private LoseWeightAdapter mNewsAdapter;
    private Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myActivity = this;
        setContentView(R.layout.activity_lose_weight);
        rvList = findViewById(R.id.rv_list);
        initView();
    }
    /**
     * 初始化页面
     */
    private void initView() {
        LinearLayoutManager layoutManager=new LinearLayoutManager(myActivity);
        //=1.2、设置为垂直排列，用setOrientation方法设置(默认为垂直布局)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //=1.3、设置recyclerView的布局管理器
        rvList.setLayoutManager(layoutManager);
        //==2、实例化适配器
        //=2.1、初始化适配器
        mNewsAdapter=new LoseWeightAdapter();
        //=2.3、设置recyclerView的适配器
        rvList.setAdapter(mNewsAdapter);
        loadData();
    }
    private void loadData(){
        String url="http://v.juhe.cn/toutiao/index";
        Map<String, Object> map=new HashMap<>();
        map.put("type","jiankang");
        map.put("page",1);
        map.put("page_size",1000);
        map.put("key","e3399a2c4d23ae4d3f97f27959d1fa6e");
        //map.put("key","27bb0b0691754637e1ef40bf25f806ff");
        OkHttpTool.httpGet(url, map, new OkHttpTool.ResponseCallback() {
            @Override
            public void onResponse(final boolean isSuccess, final int responseCode, final String response, Exception exception) {
                myActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (isSuccess && responseCode==200){
                            try {
                                JSONObject jsonObject=new JSONObject(response);
                                String result=jsonObject.getString("result");
                                JSONObject jsonResult=new JSONObject(result);
                                String data=jsonResult.getString("data");
                                Type type=new TypeToken<List<LoseWeightBean>>(){}.getType();//列表信息
                                List<LoseWeightBean> list=gson.fromJson(data,type);
                                if (list.size() > 0){
                                    mNewsAdapter.addItem(list);
                                }
                                Toast.makeText(myActivity,"加载成功", Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        });
    }

    //返回
    public void back(View view){
        finish();
    }
}
